<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<#include "/admin/include/head.htm">
<script type="text/javascript">
	var goodsGrid;
	var goodsTagGrid;
	$(function(){
		//goods
		 goodsGrid = $("#maingrid2").ligerGrid({
		 	title:"商品列表",
            columns: [
				{display: 'ID', name: 'id', width:50, align: 'center', isAllowHide: false},
				{display: '商品名称', name: 'name', align: 'center', width: 200 ,render:function(row){
					return "<a target='_blank' style='color:blue' href='/goods/"+row["id"]+".jhtml'>"+row["name"]+"</a>";
				}},
				{display: '商品编码', name: 'code', align: 'center', width: 100 },
				{display: '商品分类', name: 'goodsCategory.name', align: 'center', width: 120 },
				{display: '规格', name: 'spec', align: 'center', width: 100 },
				{display: '生产厂商', name: 'manufacturer', align: 'center', width: 200},
				{display: '批准文号', name: 'authorizeNumber', align: 'center', width: 150},
				{display: '包装单位', name: 'packingUnit', align: 'center', width: 100},
				{display: '销售价', name: 'marketPrice', align: 'center', width: 80}
			],  pageSize:20, url: base+'/admin/b2c/goods/queryData.jhtml', width: '100%', height:'100%', enabledSort: true
        });
        //绑定搜索按钮
		LG.search(goodsGrid);
		
		
		
		//tag goods
		goodsTagGrid = $("#maingrid").ligerGrid({
			title:"<span style='color:green'>已关联商品</span>",
            columns: [
				{hide: true, name: 'id', width:50, align: 'center', isAllowHide: false},
				{hide: true, name: 'goods.id', width:50, align: 'center', isAllowHide: false},
				{display: '商品编码', name: 'goods.code', align: 'center', width: 100 },
				{display: '商品名称', name: 'goods.name', align: 'center', width: 200,render:function(row){
					return "<a target='_blank' style='color:blue' href='/goods/"+row["goods.id"]+".jhtml'>"+row["goods.name"]+"</a>";
				} },
				{display: '商品分类', name: 'goods.goodsCategory.name', align: 'center', width: 120 },
				{display: '排序', name: 'priority', align: 'center', width: 120 ,render:function(row,index,val){
					return "<input style='width:100px;height:20px;' val='"+val+"' onchange='editSort("+row["id"]+",this)'  value='"+val+"'/>";				
				}}
			],  pageSize:20, enabledEdit:true,url: base+'/admin/goodsTag/queryGoodsData.jhtml?tagId=${tagId}&sortname=priority&sortorder=desc',
			toolbar:{items:[{ text: '删除', click: f_delete, icon: 'remove'},{ text: '刷新', click: refresh, icon: 'refresh'}]},
			 width: '100%', height:'100%', enabledSort: true
        });
        $("#pageloading").hide();
		//绑定搜索按钮
		search(goodsTagGrid);
		$("#search_btn_reset2").css("display", "none");
		
		$("div.l-icon.l-icon-remove").html('<img src="/resource/images/icons/silkicons/delete.png" width="15px" height="15px">');
		$("div.l-icon.l-icon-refresh").html('<img src="/resource/thirdparty/ligerUI/skins/icons/32X32/refresh.gif" width="15px" height="15px">');
	});
	
	//修改排序
	function editSort(id, input){
		var val = $(input).val();
		if(/\d+/.test(val)){
			LG.ajax({
				url: base + '/admin/goodsTag/sort/'+id+'/'+val+'.jhtml',
	            success: function (data) {
					LG.showSuccess(data.content);
					$(input).attr("val",val);
	            },
	            error: function (message) {
					LG.showError(message);
	            }
	        });
        }else{
        	LG.showError("排序序号格式不正确");
        	$(input).val($(input).attr("val"));
        }
	}
	
	//关联
	function correlate(){
		var selecteds = goodsGrid.getSelecteds();
	    if (selecteds.length > 0) {
	    	jQuery.ligerDialog.confirm('确定关联这些商品吗', function (confirm) {
      			if (confirm){
					LG.ajax({
						url: base + '/admin/goodsTag/correlate.jhtml',
			            data: { tagId:'${tagId}', goodsIds: LG.getIds(selecteds) },
			            success: function () {
							LG.showSuccess('关联成功');
			                f_reload('maingrid');
			            },
			            error: function (message) {
							LG.showError(message);
			            }
			        });
			    }
			});
		} else {
			LG.tip('请在商品列表选择关联商品!');
	    }
	}
	
	//删除
	function f_delete(){
		var selecteds = goodsTagGrid.getSelecteds();
	    if (selecteds.length > 0) {
	    	jQuery.ligerDialog.confirm('确定删除吗？', function (confirm) {
      			if (confirm){
					LG.ajax({
						url: base + '/admin/goodsTag/delete.jhtml',
			            loading: '正在删除中...',
			            data: { ids: LG.getIds(selecteds) },
			            success: function () {
							LG.showSuccess('删除成功');
			                f_reload();
			            },
			            error: function (message) {
							LG.showError(message);
			            }
			        });
			    }
			});
		} else {
			LG.tip('请在已关联列表选择行!');
	    }
	}
	
	function refresh(){
		f_reload('maingrid');
	}
	
	//tag goods search
	function search(grid, p){
		p = $.extend({
			beforeSearch: null, //搜索之前调用的函数
			afterSearch: null //搜索之后调用的函数
		}, p || {});
		$("#functions input").keydown(function(e){
			if(e.keyCode==13){
				$("#search2").click();
			}
		});
		$("#search2").click(function(){
			var self = this;
			this.p = p;
			if ($.isFunction(self.p.beforeSearch)) {
				self.p.beforeSearch();
			}
			var searchData = getWarpPara("functions2");
			delete searchData[""];
			var flag = false;
			for (var key in searchData) {
				if (searchData[key].trim() != "") {
					flag = true;
					break;
				}
			}
			if (flag) {
				$("#search_btn_reset2").css("display", "inline");
				grid.options.parms = searchData;
				grid.options.newPage=1; //将搜索页变成第一页
				grid.loadData(true);
				if ($.isFunction(self.p.afterSearch)) {
					self.p.afterSearch();
				}
			}
		});
		$("#search_btn_reset2").click(function(){
			clearSearchWeiget("functions2");
			grid.options.parms = null;
			grid.loadData(true);
			$(this).css("display", "none");
		});
	};
</script>
</head>
<body style="padding:10px; overflow:hidden;">
	<div>
		<div style="margin:0; padding:0; float: left;width:42%;">
			<div id="functions">
				<div class="clearfix">
					<div style="float:left;">
						<span>
							商品名称：<input type="text" class="formText" name="search_LIKE_name"/>
						</sapn>
						<span>
							商品编码：<input type="text" style="width:100px;" class="formText" name="search_LIKE_code"/>
						</sapn>
						<input type="button" class="button" value="搜索" id="search"/>
						<a href="#" id="search_btn_reset" face="Wingdings 3">清除本次搜索</a>
					</div>
				</div>
			</div>
			<div id="maingrid2" style="margin:0; padding:0; "></div>
		</div>
		
		<div style="padding:200px 10px 0px 10px; float:left;width: 5%">
			<input style="width:60px;height: 30px;background: #F4D08A" type="button" onclick="correlate()" value="关联>>" />
		</div>
		
		<div style="margin:0; padding:0;float: right;width:50%;">
			<div id="functions2">
				<div class="clearfix">
					<div style="float:left;">
						<span>
							商品名称：<input type="text" class="formText" name="search_LIKE_goods.name"/>
						</sapn>
						<span>
							商品编码：<input type="text" class="formText" name="search_LIKE_goods.code"/>
						</sapn>
						<input type="button" class="button" value="搜索" id="search2"/>
						<a href="#" id="search_btn_reset2" face="Wingdings 3">清除本次搜索</a>
					</div>
				</div>
			</div>
			<div id="maingrid" style="margin:0; padding:0;"></div>
		</div>
	</div>
	
</body>
</html>